package com.base.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.servlet.ModelAndView;

import com.base.po.voip.SelfShopCoupon;
import com.base.po.voip.Subscribers;
import com.base.po.voip.Users;
import com.base.service.SelfShopCouponService;
import com.base.service.UsersService;
import com.base.utils.Page;

/**
 * 自营商城优惠券
 * 
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/admin")
public class index_SelfShopCoupon {
	@Autowired
	private SelfShopCouponService selfShopCouponService;
	@Autowired
	private UsersService usersService;

	// 优惠券首页
	@RequestMapping("/selfShopCoupon")
	public ModelAndView index(HttpServletRequest request, String batch) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		Page page = new Page();
		Users users = usersService.selectByPrimaryKey(adminUserId);
		//优惠券
		List<SelfShopCoupon> selfShopCoupon = null;
		page.setCurrentPageNo(1);
		if(users.getRole() == 0 || users.getRole() == 1) {
			page.setRecordTotal(selfShopCouponService.countAllSelfShopCoupon(adminUserId, adminUserId, batch, null, null));
			selfShopCoupon = selfShopCouponService.selectAllSelfShopCoupon(page, adminUserId,adminUserId, batch, null, null);
		}
		if(users.getRole() == 2) {
			page.setRecordTotal(selfShopCouponService.countAllSelfShopCoupon(users.getParentid(), adminUserId, batch, null, null));
			selfShopCoupon = selfShopCouponService.selectAllSelfShopCoupon(page, users.getParentid(),adminUserId, batch, null, null);
		}
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中
		request.setAttribute("selfShopCoupon", selfShopCoupon);
		request.setAttribute("batch", batch);
		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_SelfShopCoupon");
		return modelAndView;
	}
	
	@RequestMapping("/selectSelfShopCoupon")
	public ModelAndView selectSelfShopCoupon(HttpServletRequest request, Integer curPage,
			 String batch, String mobile, Byte status) throws Exception {
		// 设置session缓存
		if (curPage == null) {
			if (request.getSession().getAttribute("curPage") != null) {
				curPage = (Integer) request.getSession().getAttribute("curPage");
			} else {
				curPage = 1;
			}
		}
//		Integer adminUsergrade = (Integer) request.getSession().getAttribute("adminUsergrade");
		String adminUserId = (String) request.getSession().getAttribute("adminUserId");
	
		ModelAndView modelAndView = new ModelAndView();
		Page page = new Page();
        Users users = usersService.selectByPrimaryKey(adminUserId);
		
		//优惠券
        List<SelfShopCoupon> selfShopCoupon = null;
		Integer choose = 2;
		if(mobile != null && !mobile.equals("")  ||status != 3) {
			choose = 1;
		}
		
		String fMobile = null;
		Byte fStatus = null;
		
		if(mobile != null && !mobile.equals("")) {
			fMobile = mobile;
		}
		if(status != 3) {
			fStatus = status;
		}
		
		page.setCurrentPageNo(curPage);
		if(users.getRole() == 0 || users.getRole() == 1) {
			page.setRecordTotal(selfShopCouponService.countAllSelfShopCoupon(adminUserId, adminUserId,  batch, fMobile, fStatus));
			selfShopCoupon = selfShopCouponService.selectAllSelfShopCoupon(page, adminUserId,adminUserId,  batch, fMobile, fStatus);
		}
		if(users.getRole() == 2) {
			page.setRecordTotal(selfShopCouponService.countAllSelfShopCoupon(users.getParentid(), adminUserId, batch, fMobile, fStatus));
			selfShopCoupon = selfShopCouponService.selectAllSelfShopCoupon(page, users.getParentid(),adminUserId, batch, fMobile, fStatus);
		}
		
		if(choose == 2) {
			if(users.getRole() == 0 || users.getRole() == 1) {
				page.setRecordTotal(selfShopCouponService.countAllSelfShopCoupon(adminUserId, adminUserId, batch, null, null));
				selfShopCoupon = selfShopCouponService.selectAllSelfShopCoupon(page, adminUserId,adminUserId, batch,null, null);
			}
			if(users.getRole() == 2) {
				page.setRecordTotal(selfShopCouponService.countAllSelfShopCoupon(users.getParentid(), adminUserId, batch, null, null));
				selfShopCoupon = selfShopCouponService.selectAllSelfShopCoupon(page, users.getParentid(),adminUserId, batch, null, null);
			}
		}
		
		request.setAttribute("totlenum", page.getRecordTotal()); // 分页
		request.setAttribute("curPage", page.getCurrentPageNo());// 当前页面传入session中

		// 设置页面进行跳转
		modelAndView.setViewName("jsp/index_SelfShopCoupon");
		request.setAttribute("mobile", mobile);
		request.setAttribute("status", status);
		request.setAttribute("selfShopCoupon", selfShopCoupon);
		request.setAttribute("batch", batch);
		return modelAndView;
	}
}
